package com.hsy.bibc.modules.common;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.hsy.bibc.R;

/**
 * Created by hsy on 2016/6/3.
 */
public class Numberator extends LinearLayout implements View.OnClickListener {
    public interface OnNumberChangedListener {
        void onNumberChanged(int result);
    }

    private Button leftButton, rightButton;
    private EditText editText;

    private int min = 0, max = 100;

    public OnNumberChangedListener getOnNumberChangedListener() {
        return mOnNumberChangedListener;
    }

    public void setOnNumberChangedListener(OnNumberChangedListener mOnNumberChangedListener) {
        this.mOnNumberChangedListener = mOnNumberChangedListener;
    }

    private OnNumberChangedListener mOnNumberChangedListener;

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public Numberator(Context context) {
        super(context);
        init(context);
    }

    public Numberator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Numberator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View rootView = inflate(getContext(), R.layout.numerator_layout, null);

        leftButton = (Button) rootView.findViewById(R.id.left_button);
        leftButton.setOnClickListener(Numberator.this);
        rightButton = (Button) rootView.findViewById(R.id.right_button);
        rightButton.setOnClickListener(Numberator.this);

        editText = (EditText) rootView.findViewById(R.id.edit_text);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (null != mOnNumberChangedListener) {
                    mOnNumberChangedListener.onNumberChanged(Integer.parseInt(s.toString()));
                }
            }
        });

        addView(rootView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void onClick(View view) {
        int n = Integer.parseInt(editText.getText().toString());
        if (view.getId() == R.id.left_button) {
            if (n < getMax()) {
                editText.setText(String.valueOf(++n));
            }
        } else if (view.getId() == R.id.right_button) {
            if (n > getMin()) {
                editText.setText(String.valueOf(--n));
            }
        }
    }
}
